<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入任务标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:task:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:task:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:task:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:task:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="id" width="60" />
      <!-- <el-table-column label="专业" align="center" prop="subjectDeptId" />
      <el-table-column label="课程" align="center" prop="subjectId" />
      <el-table-column label="章节" align="center" prop="subjectChapterDeptId" /> -->
      <!-- <el-table-column prop="subjectChapterDeptId" label="专业" width="120" align="center">
        <template slot-scope="scope"> 
          {{ scope.row.deptName||'-'  }}
        </template>
      </el-table-column> -->
      <el-table-column prop="subjectChapterDeptId" label="课程" width="120" align="center">
        <template slot-scope="scope"> 
          {{ scope.row.subjectName||'-'  }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="subjectChapterDeptId" label="章节" width="120" align="center">
        <template slot-scope="scope"> 
          {{ scope.row.chapterName||'-'  }}
        </template>
      </el-table-column> -->
      <el-table-column label="标题" align="center" prop="title" width="180" /> 
      <el-table-column prop="day" label="天数"  align="center"  width="100">
        <template slot-scope="scope">
          第{{ scope.row.day }}天任务
        </template>
      </el-table-column>
      <el-table-column prop="type" label="课程类型" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1">选项题</el-tag>
          <el-tag type="success" v-if="scope.row.type == 2">图片题</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="内容" align="center" prop="content" />  --> 
      <el-table-column label="合格分数" align="center" prop="score" />
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  fixed="right" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-tickets" @click="bindBank()"  v-if="scope.row.type == 1"
                v-hasPermi="['system:task:edit']">题目添加</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:task:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:task:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改科目任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务" prop="title">
          <el-input v-model="form.title" placeholder="请输入任务标题" />
        </el-form-item>
        <el-form-item label="副标题" prop="subTitle">
          <el-input v-model="form.subTitle" placeholder="请输入任务副标题" />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="任务类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
          
        </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="任务天数" prop="day">
          <el-input v-model="form.day" placeholder="请输入任务天数" type="number" />
        </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.type==1">
            <el-form-item label="合格分数" prop="score">
          <el-input v-model="form.score" placeholder="请输入合格分数" />
        </el-form-item>
          </el-col>
        </el-row>
       
      
       
        <el-form-item label="内容" v-if="form.type==2">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item> 
        <!-- <el-form-item label="subjectId" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="subjectDeptId" prop="subjectDeptId">
          <el-input v-model="form.subjectDeptId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="subjectChapterDeptId" prop="subjectChapterDeptId">
          <el-input v-model="form.subjectChapterDeptId" placeholder="请输入${comment}" />
        </el-form-item> -->
    
        <el-form-item label="参考内容" prop="answer">
          <editor v-model="form.answer" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 题目绑定抽屉 -->
    <el-drawer :visible.sync="drawer" :direction="direction"   size="70%"  title="题目绑定"   :append-to-body="true" >
      <div style="padding: 16px; width: 100%;" v-if="drawer">

        <bind  :deptId ="subjectDeptId"   :subId ="subjectId" />
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/course/task";
import bind from "./bind";

export default {
  name: "Task",
  components: { bind },
  dicts: ['sys_normal_disable'],
  props: {
    subId: {
      type: Number
    },
    deptId: {
      type: Number
    }
  },
  data() {
    return { 
      
      subjectId: this.subId,
      subjectDeptId: this.deptId,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      types: [
        {value:1,label:'选项题'},
        {value:2,label:'图片题'},
      ],
      // 非单个禁用
      drawer: false,
      direction: 'rtl',
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科目任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        subTitle: null,
        day: null,
        content: null,
        type: null,
        status: null,
        subjectId: this.subId,
        subjectDeptId: null,
        subjectChapterDeptId: null,
        score: null,
        answer: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    bindBank(){
      this.drawer  = true
    },
    /** 查询科目任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        subTitle: null,
        day: null,
        content: null,
        type: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        status: null,
        subjectId: null,
        subjectDeptId: null,
        subjectChapterDeptId: null,
        score: 90,
        answer: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.type =1;
      this.form.subjectId = this.subjectId;
      this.form.subjectDeptId = this.subjectDeptId;
      console.log('=====')
      console.log(this.subjectId)
      console.log( this.subjectDeptId)
      this.open = true;
      this.title = "添加科目任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改科目任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除科目任务编号为"' + ids + '"的数据项？').then(function () {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
